require("prefabutil")

local prefabs =
{
    "collapse_small",
}

local function onworkfinished(inst)
    inst.components.lootdropper:DropLoot()
    local fx = SpawnPrefab("collapse_small")
    fx.Transform:SetPosition(inst.Transform:GetWorldPosition())
    fx:SetMaterial("wood")
    inst:Remove()
end

local function MakeLight()
    local assets =
    {
        Asset("ANIM", "anim/Waterwheel.zip"),
    }

    local function fn()
        local inst = CreateEntity()

        inst.entity:AddTransform()
        inst.entity:AddAnimState()
        inst.entity:AddSoundEmitter()
        inst.entity:AddMiniMapEntity()
        inst.entity:AddLight()
        inst.entity:AddNetwork()

        inst:SetDeploySmartRadius(0.5)

        -- MakeObstaclePhysics(inst, .4)
        MakeWaterObstaclePhysics(inst, 0.4, 0.5, 0.75)

        -- inst.MiniMapEntity:SetIcon("bboy_river_lantern.png")

        inst.AnimState:SetBank("Waterwheel")
        inst.AnimState:SetBuild("Waterwheel")
        inst.AnimState:PlayAnimation("idle_loop", true)

        inst.Light:SetColour(1, 0.5, 0) -- 橙色光
        inst.Light:SetRadius(2.5)       -- 设置光照范围
        inst.Light:SetFalloff(0.85)     -- 设置光照衰减率
        inst.Light:SetIntensity(0.85)   -- 设置光照强度
        inst.Light:Enable(true)

        -- inst.AnimState:SetBloomEffectHandle("shaders/anim.ksh") -- 加个泛光,游戏中可以在官方设置中开启关闭

        inst:AddTag("structure")
        -- inst:AddTag("lamp")
        -- inst:AddTag("decortable")

        MakeSnowCoveredPristine(inst)

        MakeInventoryFloatable(inst, "med", 0, { 2, 0.65, 1 })
        inst.components.floater:SetIsObstacle()

        local land_time = (POPULATING and math.random() * 5 * FRAMES) or 0
        inst:DoTaskInTime(land_time, function(inst)
            inst.components.floater:OnLandedServer()
        end)

        inst.entity:SetPristine()

        if not TheWorld.ismastersim then
            return inst
        end

        MakeHauntableWork(inst)
        MakeSnowCovered(inst)

        inst:AddComponent("workable")
        inst.components.workable:SetWorkAction(ACTIONS.HAMMER)
        inst.components.workable:SetWorkLeft(4)
        inst.components.workable:SetOnFinishCallback(onworkfinished)

        inst:AddComponent("inspectable")

        inst:AddComponent("lootdropper")

        return inst
    end

    return Prefab("waterwheel", fn, assets, prefabs)
end

return MakeLight(),
    MakePlacer("waterwheel_placer", "waterwheel", "waterwheel", "idle_loop", false, false, false, nil, nil, nil, nil, 2.8)
